using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;
using BitmapFont = FlatRedBall.Graphics.BitmapFont;
using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;

using NereusGame.Entities.HBodies;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;


#endif

using NereusGame.Entities.Weapons;

namespace NereusGame.Entities.Ships
{
	public partial class Ship
	{
        protected Emitter thruster;
        protected const float FRICTION = .4f;         // Initial friction on the player
        protected const string CONTEN_MANAGER = "NereusGameContent";   // Name of the contentManager
        public float CurrentHealth;
        public float MaxHealth; 
        public Weapon[] weaponInventory;                              // Weapon slots for the ship
        public int weaponSelector;

        protected enum ShipState
        {
            Alive,
            Dead
        }
        protected ShipState state;              // state of the ship
        
		private void CustomInitialize()
		{
            thruster = new Emitter();
            CurrentHealth = MaxHealth = 1;
            weaponSelector = 0;
            Body.Visible = false;
            Drag = FRICTION;
            InitializeThruster();
		}

		private void CustomActivity()
		{
            WeaponActivity();
            CheckHealth();
            if (AccelWithoutGravity.Length() > 10)
            {
                thruster.Emit();
            }
		}

		private void CustomDestroy()
		{
            if (weaponInventory != null)
            {
                for (int i = 0; i < weaponInventory.Length; i++)
                {
                    if (weaponInventory[i] != null)
                    {
                        weaponInventory[i].Destroy();
                    }
                }
            }
            base.Destroy();
		}

        private static void CustomLoadStaticContent(string contentManagerName)
        {


        }

        private void WeaponActivity()
        {
            if (weaponInventory != null)
            {
                for (int i = 0; i < weaponInventory.Length; i++)
                    weaponInventory[i].Activity();
            }
        }

        // When health of the ship is below Zero then change state to DEAD
        private void CheckHealth()
        {
            if (CurrentHealth <= 0)
                state = ShipState.Dead;
        }

        // Rotate the ship when its accelerating
        protected void RotationByAcceleration()
        {
            if(Acceleration.Length() > .01f)
                Body.RelativeRotationZ = Tex.RelativeRotationZ = (float)(Math.Atan2(Acceleration.Y, Acceleration.X) - (Math.PI*.5f));
        }

        protected void InitializeThruster()
        {
            thruster.Texture = OrangeThrust;
            thruster.NumberPerEmission = 2;
            thruster.RemovalEvent = Emitter.RemovalEventType.Alpha0;

            thruster.RelativePosition.Y = -1;
            thruster.EmissionSettings.RadialVelocityRange = 10f;
            thruster.EmissionSettings.RadialVelocity = 10f;
            thruster.EmissionSettings.Drag = .9f;
            thruster.EmissionSettings.AlphaRate = -3f;
            thruster.EmissionSettings.VelocityRangeType = RangeType.Wedge;
            thruster.EmissionSettings.BlendOperation = FlatRedBall.Graphics.BlendOperation.Add;
            thruster.EmissionSettings.ScaleX = .5f;
            thruster.EmissionSettings.ScaleY = .5f;
            thruster.EmissionSettings.YVelocityRange = 5f;
            thruster.EmissionSettings.ScaleXVelocity = .5f;
            thruster.EmissionSettings.ScaleYVelocity = .5f;
            thruster.EmissionSettings.WedgeAngle = (float)-Math.PI / 2f;
            thruster.EmissionSettings.WedgeSpread = (float)Math.PI / 10f;
            thruster.AttachTo(Body, false);
        }

        // Returns true when the ship is alive
        public bool IsAlive()
        {
            if (state == ShipState.Alive)
                return true;
            return false;

        }


        // shoot the current selected weapon.
        public virtual void Shoot()
        {
            // shoot the weapon
            weaponInventory[weaponSelector].Shoot(this, Position, (Body.Radius + 1f), RotationZ, Velocity);
        }

	}
}
